/**
 * 
 */   
package com.gson.inter;  

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.gson.bean.Articles;
import com.gson.model.BaiduOrgInfo;
import com.gson.util.HttpUtil;

/**   
 * @作者 Stone
 * @创建日期 2014-6-20 下午12:25:30
 * @版本 V1.0
 * @类说明
 */
public class BaiduPlaceService {
	
	/**
	 * 根据地区和关键词进行百度Place检索
	 * @param query
	 * @param region
	 * @return
	 */
	public static BaiduOrgInfo[] placeSearch(String query, String region) {
		String rtnXml = null;
		// 百度Place检索地址
		String requestUrl = "http://api.map.baidu.com/place/v2/search?ak=mGW8G2Gfg08CuO3bZBj6Iy9i&output=json&query={QUERY}&page_size=5&page_num=0&scope=2&region={REGION}";
		// 对地区、关键词进URL编码
		requestUrl = requestUrl.replace("{QUERY}", HttpUtil.urlEncodeUTF8(query));
		requestUrl = requestUrl.replace("{REGION}", HttpUtil.urlEncodeUTF8(region));
		// 处理url中间的空格
		requestUrl = requestUrl.replaceAll("\\+", "%20");

		// 查询并获取返回结果
		try {
			rtnXml = HttpUtil.getHttpResult(requestUrl);
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject jsonObject = JSONObject.fromObject( rtnXml );
		JSONArray jsonArray = jsonObject.getJSONArray("results");
		BaiduOrgInfo[] orgArray = (BaiduOrgInfo[]) jsonArray.toArray(jsonArray, BaiduOrgInfo.class);

		return orgArray;
	}
	
	public static List<Articles> searchBaiduOrgInfo(String query, String region) {
		List<Articles> articleList = new ArrayList<Articles>();
		Articles articles = null;
		StringBuilder sb = null;
		
		BaiduOrgInfo[] orgArray = placeSearch(query, region);
		if(orgArray!=null && orgArray.length>0) {
			for (BaiduOrgInfo orgInfo : orgArray) {
				if(orgInfo.getNum()==0){
					articles = new Articles();
					sb = new StringBuilder();
					//设置Title
					articles.setTitle(orgInfo.getName());
					//设置Description
					if(StringUtils.isNotEmpty(orgInfo.getAddress())){
						sb.append("地址：").append(orgInfo.getAddress());
						sb.append("\r\n");
						sb.append("\r\n");
					}
					
					if(StringUtils.isNotEmpty(orgInfo.getTelephone())){
						sb.append("电话：").append(orgInfo.getTelephone());
						sb.append("\r\n");
					}
					articles.setDescription(sb.toString());
					//设置Url
					if(orgInfo.getDetail_info()!=null){
						sb = new StringBuilder();
						sb.append("http://map.baidu.com/detail?qt=ninf&uid=");
						sb.append(orgInfo.getUid());
						sb.append("&detail=");
						sb.append(orgInfo.getDetail_info().getType());
						articles.setUrl(sb.toString());
					}
				} else {
					System.out.println("输入地区异常");
				}
				articleList.add(articles);
			}
			
		}
		return articleList;
	}
	
	public static String queryBaiduOrgInfo(String query, String region) {
		StringBuilder sb = new StringBuilder();

		BaiduOrgInfo[] orgArray = placeSearch(query, region);
		if (orgArray != null && orgArray.length > 0) {
			for (BaiduOrgInfo orgInfo : orgArray) {
				if (orgInfo.getNum() == 0) {
					// 设置Title
					sb.append(orgInfo.getName());
					sb.append("\r\n");
					sb.append("\r\n");
					
					if (StringUtils.isNotEmpty(orgInfo.getAddress())) {
						sb.append(orgInfo.getAddress());
						sb.append("\r\n");
						sb.append("\r\n");
					}

					if (StringUtils.isNotEmpty(orgInfo.getTelephone())) {
						sb.append(orgInfo.getTelephone());
						sb.append("\r\n");
						sb.append("\r\n");
					}
					
					// 设置Url
					if (orgInfo.getDetail_info() != null) {
						String url = "http://map.baidu.com/detail?qt=ninf&uid=" + orgInfo.getUid() + "&detail=" + orgInfo.getDetail_info().getType();
						sb.append("<a href=\"" + url + "\">查看详情</a>");
						sb.append("\r\n");
						sb.append("\r\n");
					}
					sb.append("\r\n");
				}

			}
		}
		return sb.toString();
	}

	// 测试方法
	public static void main(String[] args) {
		BaiduOrgInfo[] orgArray = placeSearch("教育局", "成都");
		if(orgArray!=null && orgArray.length>0) {
			for (BaiduOrgInfo orgInfo : orgArray) {
				if(orgInfo.getNum()==0){
					System.out.println("name：" + orgInfo.getName());
					System.out.println("address：" + orgInfo.getAddress());
					System.out.println("telephone：" + orgInfo.getTelephone());
				} else {
					System.out.println("输入地区异常");
					return;
				}
				
			}
		}
	}

}
